<?php

class Generator_Validator_Tracklist extends Kernel_Validator_Abstract 
{
    
    const EMPTY_FILE = 'EMPTY_FILE';
    
    const NO_ROW_WAS_FOUND = 'NO_ROW_WAS_FOUND';
    
    
    public $separator;
    
    /**
     * Проверяет правильность указанного значения
     *
     * @param   Kernel_Form_Value_File   $file
     * @throws  Kernel_Validator_Exception
     */
    protected function _validate($file) 
    {
        $this->_empty($file);
        
        $this->_hasRow($file);
    }
    
    /**
    * Return whether given file is empty
    * 
    * @param Kernel_Form_Value_File $file
    */
    private function _empty($file) 
    {
        if (!(bool)count(file($file->getPath()))) {
            $this->_error(__(self::EMPTY_FILE));
        }
    }
    
    /**
    * Return whether given file contains any tracklist row
    * 
    * @param Kernel_Form_Value_File $file
    */
    private function _hasRow($file) 
    {
        foreach(file($file->getPath()) as $row) {
            if (false !== strpos($row, $this->separator)) {
                return;
            }
        }
        $this->_error(__(self::NO_ROW_WAS_FOUND));
    }
    
}